<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<html>
    <head>
        <title>OSDiary</title>
        <link href="css/default.css" rel="stylesheet" type="text/css" />
        <meta content="text/html; charset=utf-8" http-equiv="content-type">
        <script src="js/prototype.js" type="text/javascript"></script>
        <sx:head />
    </head>
    <body>

        <script>
            <!--
            function showProperForm(race) {
                new Ajax.Request(race+'ShowForm.action',{
                    method: 'get',
                    parameters: 'eventId=<s:property value="eventId"/>',
                    onSuccess: function(transport){
                        var div = $('divForForm');
                        var link = $(race+'Link');
                        div.innerHTML = transport.responseText;
                        link.innerHTML = '';
                    }
                });
            }

            function saveProperRace(race){
                var parametersString = '';
                parametersString += 'eventId=<s:property value="eventId"/>';
                parametersString += '&distance=' + $F('distance');
                parametersString += '&duration=' + $('duration').getValue();
                parametersString += '&averagePulse=' + $('average_pulse').getValue();
                parametersString += '&maxPulse=' + $('max_pulse').getValue();
                parametersString += '&minPulse=' + $('min_pulse').getValue();
                parametersString += '&climb=' + $('climb').getValue();
                parametersString += '&description=' + $('description').getValue();

                if (race == 'ORace') {
                    parametersString += '&controls_count=' + $('controls_count').getValue();
                    parametersString += '&map_link=' + $('map_link').getValue();
                }
                new Ajax.Request('save'+race+'.action',{
                    method: 'get',
                    parameters: parametersString,
                    onSuccess: function(transport){
                        var div = $('divForForm');
                        var link = $(race+'Link');
                        if(transport.responseText.match(/added/)) {
                            div.innerHTML = 'Added';
                            link.innerHTML = 'Add'+race;
                        } else {
                            div.innerHTML = 'Not added';
                        }
                    }
                });
            }

            function showEventEditForm(){
                var div = $('eventDiv');
                var eventId = '<s:property value='eventId'/>';
                var name = encodeURIComponent('<s:property value="name"/>');
                var place = encodeURIComponent('<s:property value="place"/>');
                var date = '<s:property value="dateTimeString"/>';
                var parametersString = 'name=' + name;
                parametersString += '&place=' + place;
                parametersString += '&dateTimeString=' + date;
                parametersString += '&eventId=' + eventId;
                new Ajax.Request('EventEditShowForm.action',{
                    method: 'getparameters:',
                    parameters: parametersString,
                    onSuccess: function(transport){
                        div.innerHTML = transport.responseText;
                    }
                });
            }

            function validateRunRace() {
                new Ajax.Request(
                'validateAddRunRace.action',
                {
                    method: 'get',
                    parameters: Form.serialize($('form')) + '&struts.enableJSONValidation=true&struts.validateOnly=true',
                    onComplete: postValidation
                }
            );
            }

            function postValidation(request) {
                var form = $('form');

                //clear previous validation errors, if any
                StrutsUtils.clearValidationErrors(form);

                //get errors from response
                var text = request.responseText;
                var errorsObject = StrutsUtils.getValidationErrors(text);

                //show errors, if any
                if(errorsObject.fieldErrors) {
                    StrutsUtils.showValidationErrors(form, errorsObject);
                } else {
                    //good to go, regular submit
                    form.submit();
                }
            }
            -->
        </script>

        <jsp:include page="WEB-INF/jspparts/logo.jsp" />

        <div id="content">

            <jsp:include page="WEB-INF/jspparts/sidebar.jsp" />

            <div id="main">
                <div id="welcome" class="post">

                    <div id="eventDiv">
                        <table>
                            <tr>
                                <td><s:property value="%{getText('call')}"/></td>
                                <td><s:property value="name"/></td>
                            </tr>
                            <tr>
                                <td><s:property value="%{getText('place')}"/></td>
                                <td><s:property value="place"/></td>
                            </tr>
                            <tr>
                                <td><s:property value="%{getText('date')}"/></td>
                                <td><s:property value="dateTimeString" /></td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <a onclick="showEventEditForm()"><s:property value="%{getText('editEvent')}"/></a>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <br />
                    <hr>
                    <table>
                        <tr>
                            <td colspan="2">
                                RunRaces
                            </td>
                        </tr>
                        <s:iterator value="runRaces" status="statusRunRaces">
                            <tr>
                                <td><s:property value="%{getText('distance')}"/></td>
                                <td><s:property value="distance"/></td>
                            </tr>
                            <tr>
                                <td><s:property value="%{getText('duration')}"/></td>
                                <td><s:property value="duration"/></td>
                            </tr>
                            <tr>
                                <td><s:property value="%{getText('desctription')}"/></td>
                                <td><s:property value="description"/></td>
                            </tr>
                        </s:iterator>
                    </table>
                    <br />
                    <hr>
                    <table>
                        <tr>
                            <td colspan="2"/>
                        </tr>
                        <s:iterator value="runORaces" status="statusORaces">
                            <tr>
                                <td><s:property value="%{getText('distance')}"/></td>
                                <td><s:property value="distance"/></td>
                            </tr>
                            <tr>
                                <td><s:property value="%{getText('duration')}"/></td>
                                <td><s:property value="duration"/></td>
                            </tr>
                            <tr>
                                <td><s:property value="%{getText('desctription')}"/></td>
                                <td><s:property value="description"/></td>
                            </tr>
                        </s:iterator>
                    </table>
                    <br />
                    <hr>
                    <table id="addFormLinks">
                        <tr>
                            <td onclick="showProperForm('RunRace')" id="RunRaceLink">Add Run Race</td>
                        </tr>
                        <tr>
                            <td onclick="showProperForm('ORace')" id="ORaceLink">Add O-Race</td>
                        </tr>
                    </table>
                    <br />

                    <div id="divForForm"></div>

                </div>
            </div>
            <div id="extra" style="clear: both;"></div>
        </div>

        <jsp:include page="WEB-INF/jspparts/footer.jsp" />

    </body>
</html>
